/*
 * @(#) UserServiceImpl
 *
 * <br> Copyright:  Copyright (c) 2017
 * <br> @author caijinhuang
 * <br> 2017-12-18 16:24:52
 */

package com.sunsharing.sharemeeting.service.impl;

import com.sunsharing.share.common.base.IdGenerator;
import com.sunsharing.sharemeeting.dao.api.UserDao;
import com.sunsharing.sharemeeting.entity.ExcelObject;
import com.sunsharing.sharemeeting.entity.User;
import com.sunsharing.sharemeeting.service.api.UserService;
import com.sunsharing.sharemeeting.util.BaseUtil;
import com.xuxueli.poi.excel.ExcelImportUtil;

import org.apache.log4j.Logger;
import org.mybatis.spring.MyBatisExceptionTranslator;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    UserDao userDao;

    Logger logger = Logger.getLogger(UserServiceImpl.class);

    /**
     * 登陆查询
     * @param params = {
     *           username：用户账号
     *           password: 密码
     * }
     * @return
     */
    public User checkLogin(String username, String password) {
        User user = userDao.selectUserByUserName(username);
        if (user != null && user.getPassword().equals(password)) {
            return user;
        }
        return null;
    }

    /**
     * 账号检查
     * @param username 用户账号
     * @return
     */
    public User checkUser(String username) {
        return userDao.selectUserByUserName(username);
    }


    /**
     * 修改密码
     * @param username ：账号
     * @param password ：密码
     * @return
     */
    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
    public int changePassWd(String username, String password) {
        User user = new User();
        user.setPassword(password);
        user.setUsername(username);
        user.setAlterTime(new Timestamp(new Date().getTime()));
        return userDao.updateUser(user);
    }

    /**
     * 修改用户角色权限
     * @param roleId    角色ID
     * @param userIdList 待修改的用户角色id列表
     * @return
     */
    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
    public void updateUserRole(String roleId, List<String> userIdList) {
        userDao.updateUserRole(roleId, userIdList);
    }

    /**
     * 用户注册
     * @param user 注册的用户对象
     */
    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
    public void userRegist(User user) {
        Timestamp now = new Timestamp(new Date().getTime());
        user.setLoginTime(now);
        user.setAlterTime(now);
        user.setCreateTime(now);
        user.setUserId(IdGenerator.uuid2());
        userDao.insertUser(user);
    }

    /**
     * 批量导入用户数据
     * @param inputStream excel文件数据流
     * @return
     */
    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
    public String registMoreUser(InputStream inputStream) {

        List<Object> list = ExcelImportUtil.importExcel(ExcelObject.class, inputStream);
        List<User> ulist = null;
        try {
            ulist = BaseUtil.getUserList(list);
            int num = userDao.insertMoreUsers(ulist);
            if (num <= 0) {
                return "error";
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (DataAccessException e) {
            logger.info("错误信息：" + e);
            return e.getRootCause().toString();
        }
        return "success";
    }

    /**
     * 用户账号模糊搜索
     * @param username 用户账号
     * @return
     */
    public List<User> searchUser(String username) {
        return userDao.searchUserByUsername("%" + username + "%");
    }

    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
    public int deleteUser(String userId) {
        return userDao.deleteUser(userId);
    }
}
